/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AppraisalDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoanapplicationDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Appraisal;
import com.cashmate.hibernate.pojo.Collaterals;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Loanverification;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Lee
 */
public class UpdateAP extends ActionSupport implements ServletRequestAware {

    private Appraisal appr;
    private String actionstatus;
    private AppraisalDAO ad = (AppraisalDAO) ServletActionContext.getServletContext().getAttribute("appraisalDAO");
    private LoanapplicationDAO lad = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private EmployeeDAO emd = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    //loanapp
    private String loanverid;
    //appraisal
    private String accountnoapp, dateoffirstrelease, totalfairmarketvalue, totalappraisedvalue, loanableamount, employeeid;
    //document
    private String documentdateapp, docreftypeapp, docrefnoapp, docnoapp, doctypeapp;
    //collaterals
    private String[] makeandtype, brand, serialno, modelno, regcertno, motorno, chasisno, plateno, fairmarketvalue, location, gencon;
    private String successurl;
    //image
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;
    private String apid;

    @Action(value = "/update-ap", results = {
        @Result(name = "input", location = "${successurl}"),
        @Result(name = "success", location = "${successurl}"),
        @Result(name = "error", location = "${successurl}")
    })
    @Override
    public String execute() {
        successurl = "../../loanprofile-loanapp.jsp?cat=appraise&id=" + apid;
        Appraisal a = null;
        try {
            a = ad.get(Integer.parseInt(apid));
        } catch (Exception e) {
            actionstatus = "Appraisal doest not exist anymore.";
            return ERROR;
        }

        if (fairmarketvalue != null) {
            try {
                int i = Integer.parseInt(totalfairmarketvalue);
                a.setTotalfairmarketvalue(i);
            } catch (Exception e) {
                actionstatus = "Please provide a valid FMV.";
                return ERROR;
            }
        }

        if (totalappraisedvalue != null) {
            try {
                int i = Integer.parseInt(totalappraisedvalue);
                a.setTotalappraisedvalue(i);
            } catch (Exception e) {
                actionstatus = "Please provide a valid AV.";
                return ERROR;
            }
        }

        if (loanableamount != null) {
            try {
                int i = Integer.parseInt(loanableamount);
                a.setLoanableamount(i);
            } catch (Exception e) {
                actionstatus = "Please provide a valid loanable amount.";
                return ERROR;
            }
        }

        try {
            a.setStat("posted");
            ad.update(a);
            actionstatus = "Appraisal posted.";
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            actionstatus = "Error posting AP.";
            return ERROR;
        }

    }

    public String getSuccessurl() {
        return successurl;
    }

    public String getActionstatus() {
        return actionstatus;
    }

    public void setActionstatus(String actionstatus) {
        this.actionstatus = actionstatus;
    }

    public void setAccountnoapp(String accountnoapp) {
        this.accountnoapp = accountnoapp;
    }

    public void setAppr(Appraisal appr) {
        this.appr = appr;
    }

    public void setDateoffirstrelease(String dateoffirstrelease) {
        this.dateoffirstrelease = dateoffirstrelease;
    }

    public void setDocnoapp(String docnoapp) {
        this.docnoapp = docnoapp;
    }

    public void setDocrefnoapp(String docrefnoapp) {
        this.docrefnoapp = docrefnoapp;
    }

    public void setDocreftypeapp(String docreftypeapp) {
        this.docreftypeapp = docreftypeapp;
    }

    public void setDoctypeapp(String doctypeapp) {
        this.doctypeapp = doctypeapp;
    }

    public void setBrand(String[] brand) {
        this.brand = brand;
    }

    public void setChasisno(String[] chasisno) {
        this.chasisno = chasisno;
    }

    public void setFairmarketvalue(String[] fairmarketvalue) {
        this.fairmarketvalue = fairmarketvalue;
    }

    public void setGencon(String[] gencon) {
        this.gencon = gencon;
    }

    public void setLocation(String[] location) {
        this.location = location;
    }

    public void setMakeandtype(String[] makeandtype) {
        this.makeandtype = makeandtype;
    }

    public void setModelno(String[] modelno) {
        this.modelno = modelno;
    }

    public void setMotorno(String[] motorno) {
        this.motorno = motorno;
    }

    public void setPlateno(String[] plateno) {
        this.plateno = plateno;
    }

    public void setRegcertno(String[] regcertno) {
        this.regcertno = regcertno;
    }

    public void setSerialno(String[] serialno) {
        this.serialno = serialno;
    }

    public void setDocumentdateapp(String documentdateapp) {
        this.documentdateapp = documentdateapp;
    }

    public void setLoanableamount(String loanableamount) {
        this.loanableamount = loanableamount;
    }

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public void setTotalappraisedvalue(String totalappraisedvalue) {
        this.totalappraisedvalue = totalappraisedvalue;
    }

    public void setTotalfairmarketvalue(String totalfairmarketvalue) {
        this.totalfairmarketvalue = totalfairmarketvalue;
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    public void setApid(String apid) {
        this.apid = apid;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }
}
